package BVRClient;

/**
 * The Blue vs Red: Arena Rectangle Class.
 * @author Jesse Gill
 * @author Shannon Setter
 */
public class BVRC_Rect
{
	public float x = 0, y = 0;
	public float width = 0, height = 0;
	
	/**
	 * 
	 */
	public BVRC_Rect() {}
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public BVRC_Rect(int x, int y, int width, int height)
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public BVRC_Rect(float x, float y, float width, float height)
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	/**
	 * 
	 * @param r
	 * @return true if there is a collision, false if not
	 */
	public boolean rectCollision(BVRC_Rect r)
	{
		if ((this.y + this.height) < r.y)
			return false;
		
		if (this.y > (r.y + r.height))
			return false;
		
		if ((this.x + this.width) < r.x)
			return false;
		
		if (this.x > (r.x + r.width))
			return false;
		
		return true;
	}
	
	/**
	 * 
	 * @param p
	 * @return true if there is a collision, false if not
	 */
	public boolean pointCollision(BVRC_Point p)
	{
	    if((p.x > this.x) && (p.x < this.x + this.width) && (p.y > this.y) && (p.y < this.y + this.height))
	    {
	        return true;
	    }
	    
		return false;
	}
	
	/**
	 * 
	 * @param l
	 * @return true if the given line l collides with this rect, false in all other cases
	 */
	public boolean lineCollision(BVRC_Line l)
	{
		// top
		BVRC_Line rectLine1 = new BVRC_Line(this.x, this.y, this.x + this.width, this.y);
		// left
		BVRC_Line rectLine2 = new BVRC_Line(this.x, this.y, this.x, this.y + this.height);
		// right
		BVRC_Line rectLine3 = new BVRC_Line(this.x + this.width, this.y, this.x + this.width, this.y + this.height);
		// bottom
		BVRC_Line rectLine4 = new BVRC_Line(this.x, this.y + this.height, this.x + this.width, this.y + this.height);
		
		// if any of the rectangle's lines intersect with the given line
		if (l.lineIntersects(rectLine1) || 
				l.lineIntersects(rectLine2) ||
				l.lineIntersects(rectLine3) ||
				l.lineIntersects(rectLine4)
				)
		{
			// collision
			return true;
		}
		
		// no collision
		return false;
	}
}
